package com.onlyyou.cart.controller;

import com.onlyyou.cart.VO.ResultVO;
import com.onlyyou.cart.entity.Cart;
import com.onlyyou.cart.service.CartService;
import com.onlyyou.cart.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author: 小野
 * @Date: 2020/1/11 14:20
 */
@Api(tags = "购物车相关服务")
@RestController
@RequestMapping("/")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/findAll")
    public Map<String,Object> findAll(Integer page, Integer limit) {
        return cartService.findAll(page-1,limit);
    }

    @ApiOperation(value = "根据用户jsonId查询用户的购物车信息")
    @GetMapping("/query/{userId}")
    public ResultVO queryByUserId(@PathVariable("userId") Long jsonUserId) {
        Cart cart=new Cart();
        cart.setUserId(jsonUserId);
        //格式提取
        return ResultUtil.success(cartService.findByUserId(cart));
    }

    @ApiOperation(value = "向用户的购物车中添加数据信息")
    @PostMapping("/save")
    public ResultVO save( Long userid,Long stockid,String imageurl,String stockname,Long stockprice,Integer num) {
        Cart cart=new Cart();
        cart.setId(System.currentTimeMillis());
        cart.setUserId(userid);
        cart.setStockId(stockid);
        cart.setStockName(stockname);
        cart.setStockImage(imageurl);
        BigDecimal price=new BigDecimal(stockprice);
        cart.setStockPrice(price);
        cart.setNum(num);
        return ResultUtil.success(cartService.save(cart));
    }

    @ApiOperation(value = "用户更新个人购物车信息")
    @GetMapping("/update")
    public ResultVO update(Long id,Integer num) {
        Cart cart=new Cart();
        cart.setId(id);
        cart.setNum(num);
        return ResultUtil.success(cartService.update(cart));
    }

    @ApiOperation(value = "用户删除个人购物车信息")
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam Cart cart) {
        cartService.delete(cart);
        return ResultUtil.success();
    }
}
